<template>
  <div class="barometer-page">
    <main>
      <div class="search-container">
        <div class="search-item">
          <span>监测年月：</span>
          <el-date-picker
            v-model="searchDate"
            format="yyyy-MM"
            value-format="yyyy-MM"
            type="month"
            placeholder="请选择"
            :clearable="false"
            @keydown.native.enter="getEnvirHistory"
          >
          </el-date-picker>
        </div>
        <div class="search-item">
          <el-button
            v-if="hasTabExport"
            class="download-btn"
            icon="el-icon-download"
            type="primary"
            @click="getEnvirHistoryTmp"
          >
            导出
          </el-button>
          <el-button
            type="primary"
            icon="el-icon-refresh"
            @click="handleRefresh"
          >
            重置
          </el-button>
        </div>
      </div>
      <el-calendar v-model="searchDate">
        <template slot="dateCell" slot-scope="{ date, data }">
          <div :class="['cell-content', data.isSelected ? 'is-selected' : '']">
            <div class="date">
              <span>{{
                data.day
                  .split("-")
                  .slice(1)
                  .join("-")
              }}</span>
            </div>
            <div class="info">
              <p>{{ setWeather(data.day) }}</p>
              <p v-if="setTemp(data.day)">气温：{{ setTemp(data.day) }}</p>
            </div>
          </div>
        </template>
      </el-calendar>
    </main>
  </div>
</template>

<script>
import { Calendar } from "element-ui";

export default {
  components: {
    ElCalendar: Calendar
  },
  data() {
    return {
      barometerList: [],
      searchDate: this.$moment().format("YYYY-MM")
    };
  },
  computed: {
    projectId() {
      return this.$store.state.currentProjectId;
    }
  },
  watch: {
    searchDate(val) {
      this.getEnvirHistory({
        searchDate: this.$moment(val).format("YYYY-MM")
      });
    }
  },
  created() {
    this.getEnvirHistory();
  },
  methods: {
    /**
     * 获取历史天气
     */
    async getEnvirHistory() {
      const params = {
        projectId: this.projectId,
        searchDate: this.searchDate,
        pageFlag: false
      };
      const {
        success,
        data: { data }
      } = await this.$Model.getEnvirHistory(params);
      if (success && data) {
        this.barometerList = data.list || [];
      }
    },
    /**
     * 天气历史数据导出
     */
    async getEnvirHistoryTmp() {
      const params = {
        projectId: this.projectId,
        searchDate: this.searchDate,
        pageFlag: false
      };
      const href = `${this.$configs.baseURL}/bus/envir/history/downLoadXlsByJxl`;
      let urlStr = "";
      Object.keys(params).forEach((key, index) => {
        const value = params[key] === undefined ? "" : params[key];
        urlStr += `${index === 0 ? "?" : "&"}${key}=${value}`;
      });
      window.open(href + urlStr);
    },
    handleRefresh() {
      this.searchDate = this.$moment().format("YYYY-MM");
      this.getEnvirHistory();
    },
    findItem(day) {
      if (!day) return;
      return this.barometerList.find(item => {
        return String(item.date).indexOf(day) !== -1;
      });
    },
    setWeather(day) {
      const item = this.findItem(day);
      return item ? item.weather : "";
    },
    setTemp(day) {
      const item = this.findItem(day);
      return item ? `${item.temp} ℃` : "";
    }
  }
};
</script>

<style lang="scss" scoped>
@import "../../../assets/styles/main";

.barometer-page {
  width: 100%;
  height: 100%;
  background-color: $mainBg;
  padding: 30px;
  color: $fontColor;
  main {
    height: 100%;
    background: $baseBg;
    border-radius: 15px;
    position: relative;
    /deep/ .el-calendar {
      height: 100%;
      display: flex;
      flex-direction: column;
      justify-content: space-between;
      padding-top: 200px;
      .el-calendar__header {
        display: none;
      }
      .cell-content {
        height: 100%;
        width: 100%;
        position: relative;
        .date {
          position: absolute;
          right: 4px;
          top: 4px;
          font-size: 12px;
          color: $fontDColor;
        }
        .info {
          height: 100%;
          width: 100%;
          display: flex;
          flex-direction: column;
          justify-content: center;
          align-items: center;
          p {
            padding: 2px 0;
          }
        }
      }
    }
    .search-container {
      width: 100%;
      height: 80px;
      position: absolute;
      top: 0;
      left: 0;
      display: flex;
      justify-content: space-between;
      align-items: center;
      padding: 0 20px;
      border-bottom: 1px solid #f1f1f1;
    }
  }
}
</style>
